<template>
	<view class="chart">
		<view class="head">
			<SumCart :category="category"/>
		</view>
		<view class="container">
			<view class="container-head">
				<view class="container-head-left">
					<view class="container-head-left-label" :class="{'head-active': current == 0}" @click="changeChart(0)">日</view>
					<view class="container-head-left-label" :class="{'head-active': current == 1}" @click="changeChart(1)">月</view>
					<view class="container-head-left-label" :class="{'head-active': current == 2}" @click="changeChart(2)">年</view>
				</view>
				<view class="container-head-right"> 
					<uni-icons type="left" size="20" @click="prevDate"></uni-icons>
					<DatePicker v-if="showChart" class="date" :fields="category" :defaultValue='date' @change='dateTimeChange'></DatePicker>
					<uni-icons type="right" size="20" @click="nextDate"></uni-icons>
				</view>
			</view>
			<view class="">
				<Chart :option="item.option" :category="category" :params="item.params" v-for="(item,index) in list" :key="item.option.name"/>
			</view>
			<view class="" v-show="isReach">
				<Chart :option="item.option" :category="category" :params="item.params" v-for="(item,index) in list1" :key="item.option.name"/>
			</view>
		</view>
		<uni-load-more :status="status" v-if="!isReach"></uni-load-more>
	</view>
</template>

<script>
	import { getDayData1,getDayData2,getMonthData1,getMonthData2,getYearData1,getYearData2 } from './index.js'
	import moment from 'moment'
	import Chart from '@/components/custom/data-chart/index.vue'
	import SumCart from '@/components/custom/sum-card/index.vue'
	import DatePicker from '@/components/biaofun-datetime-picker/biaofun-datetime-picker.vue'
	import _data from '@/common/_data.js'
	
	export default {
		components: {
			Chart,
			SumCart,
			DatePicker
		},
		data() {
			return {
				list: [
					{
						option: {
							name: '水量',
							lineColor: '#3CA272'
						},
						params: {
							in: 'Y2G2M03LL',
							out: 'P1E1M06SSL',
						}
					},
					{
						option: {
							name: 'COD',
							lineColor: '#7B2CFF'
						},
						params: {
							in: 'Y1J1M01JKCOD',
							out: 'P1E1M01CKCOD'
						}
					},
				],
				list1: [
					{
						option: {
							name: 'TP',
							lineColor: '#03FAFB'
						},
						params: {
							in: 'Y1J1M04JKZL',
							out: 'P1E1M04CKZL'
						}
					},
					{
						option: {
							name: 'TN',
							lineColor: '#FF81C0'
						},
						params: {
							in: 'Y1J1M03JKZD',
							out: 'P1E1M03CKZD'
						}
					},
					{
						option: {
							name: 'NH3',
							lineColor: '#FA9F10'
						},
						params: {
							in: 'Y1J1M02JKAD',
							out: 'P1E1M02CKAD'
						}
					},
					{
						option: {
							name: 'pH',
						},
						params: {
							in: '',
							out: 'P1E1M05CKPH'
						}
					},
				],
				current: 0,
				category: 'day',
				date: moment().format('YYYY-MM-DD'),
				showChart: true,
				place: _data.localData('obsname'),
				status: 'more',
				isReach: false,
			}
		},
		onLoad() {
			let that = this;
			this.getData1()
		},
		onPullDownRefresh() {
			if(this.isReach) {
				this.getData2()
			}
			this.getData1()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			},1000)
		},
		// 上拉加载
		onReachBottom() {
			let that = this
			if(!that.isReach) {
				that.isReach = true;
 				that.getData2()
				uni.showNavigationBarLoading()
				that.$nextTick(() => {
				   uni.hideNavigationBarLoading()
				})
			}
		},
		methods: {
			back() {
				uni.navigateBack();
			},
			getData1() {
				if(this.current == 0) {
					getDayData1(moment(this.date).format('YYYY-MM-DD'))
				} else if(this.current == 1) {
					getMonthData1(moment(this.date).format('YYYY-MM'))
				} else if(this.current == 2) {
					getYearData1(moment(this.date).format('YYYY'))
				}
			},
			getData2() {
				if(this.current == 0) {
					getDayData2(moment(this.date).format('YYYY-MM-DD'))
				} else if(this.current == 1) {
					getMonthData2(moment(this.date).format('YYYY-MM'))
				} else if(this.current == 2) {
					getYearData2(moment(this.date).format('YYYY'))
				}
			},
			changeChart(e) {
				this.isReach = false;
				this.current = e;
				this.showChart = false;
				if(this.current == 0) {
					this.category = 'day'
					this.date = moment().format('YYYY-MM-DD')
				} else if(this.current == 1) {
					this.category = 'month'
					this.date = moment().format('YYYY-MM')
				} else if(this.current == 2) {
					this.category = 'year'
					this.date = moment().format('YYYY')
				}
				this.getData1();
				this.$nextTick(() => {
					this.showChart = true;
				})
			},
			dateTimeChange(value) {
				this.isReach = false;
				if(this.category == 'day') {
					this.date = value.YYYY + '-' + value.MM + '-' + value.DD
				} else if(this.category == 'month') {
					this.date = value.YYYY + '-' + value.MM
				} else if(this.category == 'year') {
					this.date = value.YYYY
				}
				this.getData1()
			},
			prevDate() {
				this.isReach = false;
				if(this.category == 'day') {
					this.date = moment(this.date).add(-1, 'days').format('YYYY-MM-DD')
				} else if(this.category == 'month') {
					this.date = moment(this.date).subtract(1, 'month').format('YYYY-MM')
				} else if(this.category == 'year') {
					this.date = moment(this.date).add(-1, 'years').format('YYYY')
				}
				this.getData1()
			},
			nextDate() {
				this.isReach = false;
				if(this.category == 'day') {
					this.date = moment(this.date).add(1, 'days').format('YYYY-MM-DD')
				} else if(this.category == 'month') {
					this.date = moment(this.date).add(1, 'month').format('YYYY-MM')
				} else if(this.category == 'year') {
					this.date = moment(this.date).add(1, 'years').format('YYYY')
				}
				this.getData1()
			}
		}
	}
</script>

<style>
	.chart {
		padding: 0 20rpx;
		background-color: #fff;
	}
	
	.head {
		margin-top: 10rpx;
	}
	
	.container-head {
		height: 100rpx;
		padding-top: 40rpx;
		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;
		border-bottom: 1rpx solid #eee;
		box-sizing: border-box;
	}
	
	.container-head-left {
		width: 300rpx;
		display: flex;
		justify-content: space-around;
	}
	
	.container-head-left-label {
		width: 40rpx;
		font-size: 30rpx;
		text-align: center;
	}
	
	.container-head-right {
		display: flex;
		margin-top: -10rpx;
	}
	
	.date {
		margin: 0 20rpx;
		margin-top: 14rpx;
	}
	
	.head-active {
		color: #49BEE3;		border-bottom: 1rpx solid #49BEE3;
	}
	
	.charts-box {
		width: 95%;
		margin: 20rpx auto;
		box-sizing: border-box;
		height: 500rpx;
		border-radius: 40rpx;
	}
	
	.more {
		font-size: 30rpx;
		color: red;
		text-align: center;
		height: 100rpx;
		line-height: 100rpx;
	}
</style>

<style scoped>
	/deep/ .uniui-left::before,/deep/ .uniui-right::before {
		background-color: #EEF5F7;
		color: #90D3DE !important;
	}
</style>